home *** CD-ROM | disk | FTP | other *** search
/ Best of www.BestZips.com (Collector's Edition) / Best of WWW.BESTZIPS.COM Collector's Edition (JCSM Shareware) (JCS Marketing).ISO / editors_ / ze16v250.zip / WILDCARD.C_ / WILDCARD.C
C/C++ Source or Header  |  1995-12-14  |  3KB  |  160 lines

  1. /* wildcard.c */
  2.  
  3. /* Author:
  4.  *    Guntram Blohm
  5.  *    Buchenstrasse 19
  6.  *    7904 Erbach, West Germany
  7.  *    Tel. ++49-7305-6997
  8.  *    sorry - no regular network connection
  9.  */
  10.  
  11. /* this program implements wildcard expansion for elvis/dos. It works
  12.  * like UNIX echo, but uses the dos wildcard conventions
  13.  * (*.* matches all files, * matches files without extension only,
  14.  * filespecs may contain drive letters, wildcards not allowed in directory
  15.  * names).
  16.  *
  17.  * It is also #included into ctags.c, ref.c, ...; in this case,
  18.  * we don't want a main function here.
  19.  */
  20.  
  21. #include <stdio.h>
  22. #include <string.h>
  23. #ifndef    WILDCARD_NO_MAIN
  24. # include "config.h"
  25. #endif
  26. #include "ctype.h"
  27. #ifdef    __TURBOC__
  28. #include <dir.h>
  29. #endif
  30.  
  31. #ifdef    M_I86
  32. #define    findfirst(a,b,c)    _dos_findfirst(a,c,b)
  33. #define    findnext        _dos_findnext
  34. #define    ffblk            find_t
  35. #define    ff_name            name
  36. #include <dos.h>
  37. #endif
  38.  
  39. /* Atari TOS, MWC */
  40. #ifdef M68000
  41. #include <stat.h>
  42. #include <osbind.h>
  43. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  44. #define    findnext(x)        (Fsnext())
  45. #define    ff_name    d_fname
  46. #endif
  47.  
  48. /* Atari TOS, GNU-C */
  49. #ifdef __m68k__
  50. #include <stat.h>
  51. #include <osbind.h>
  52. #define    findfirst(a,b,c)    (Fsetdta(b), (Fsfirst(a,c)))
  53. #define    findnext(x)        (Fsnext())
  54. #define    ff_name    dta_name
  55. #define    DMABUFFER struct _dta
  56. #endif
  57.  
  58. #define    MAXFILES    1000
  59.  
  60. int pstrcmp();
  61. extern char *calloc();
  62.  
  63. char *files[MAXFILES];
  64. int nfiles;
  65.  
  66. #ifndef    WILDCARD_NO_MAIN
  67.  
  68. main(argc, argv)
  69.     char **argv;
  70. {
  71.     int i;
  72.  
  73.     _ct_init("");
  74.     for (i=1; i<argc; i++)
  75.         expand(argv[i]);
  76.     if (nfiles)
  77.         printf("%s", files[0]);
  78.     for (i=1; i<nfiles; i++)
  79.     {
  80.         printf(" %s", files[i]);
  81.     }
  82.     putchar('\n');
  83.     return 0;
  84. }
  85.  
  86. #else
  87. char **wildexpand(argc, argv)
  88.     int *argc;
  89.     char **argv;
  90. {
  91.     int i;
  92.     
  93.     for (i=0; i<*argc; i++)
  94.         expand(argv[i]);
  95.     *argc=nfiles;
  96.     return files;
  97. }    
  98. #endif
  99.  
  100. expand(name)
  101.     char *name;
  102. {
  103.     char *filespec;
  104.     int wildcard=0;
  105. #if defined(M68000) || defined(__m68k__)
  106.     DMABUFFER findbuf;
  107. #else
  108.     struct ffblk findbuf;
  109. #endif
  110.     int err;
  111.     char buf[80];
  112.     int lastn;
  113.  
  114.     strcpy(buf, name);
  115.     for (filespec=buf; *filespec; filespec++)
  116.         ;
  117.  
  118.     while (--filespec>=buf)
  119.     {    if (*filespec=='?' || *filespec=='*')
  120.             wildcard=1;
  121.         if (*filespec=='/' || *filespec=='\\' || *filespec==':')
  122.             break;
  123.     }
  124.     if (!wildcard)
  125.         addfile(buf);
  126.     else
  127.     {
  128.         lastn=nfiles;
  129.         filespec++;
  130.         if ((err=findfirst(buf, &findbuf, 0))!=0)
  131.             addfile(buf);
  132.         while (!err)
  133.         {
  134.             strcpy(filespec, findbuf.ff_name);
  135.             addfile(buf);
  136.             err=findnext(&findbuf);
  137.         }
  138.         if (lastn!=nfiles)
  139.             qsort(files+lastn, nfiles-lastn, sizeof(char *), pstrcmp);
  140.     }
  141. }
  142.  
  143. addfile(buf)
  144.     char *buf;
  145. {
  146.     char *p;
  147.  
  148. //    for (p=buf; *p; p++)
  149. //        *p=tolower(*p);
  150.  
  151.     if (nfiles<MAXFILES && (files[nfiles]=calloc(strlen(buf)+1, 1))!=0)
  152.         strcpy(files[nfiles++], buf);
  153. }
  154.  
  155. int pstrcmp(a, b)
  156.     char **a, **b;
  157. {
  158.     return strcmp(*a, *b);
  159. }
  160.